'use strict';

import { invitationawardModel } from "../model/invitationaward";
import { ServiceBase, Exceptions } from '../kalend';

class InvitationawardService extends ServiceBase {
    constructor() {
        super(invitationawardModel)
    }
    async query(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        const areas = params.areas;
        const title = params.title;
        const status = params.status;

        let filter: any = {};
        if (areas && areas.length > 0) {
            switch (areas.length) {
                case 1:
                    filter.province = areas[0];
                    break;
                case 2:
                    filter.province = areas[0];
                    filter.city = areas[1];
                    break;
                case 3:
                    filter.province = areas[0];
                    filter.city = areas[1];
                    filter.district = areas[2];
                    break;
                default:
                    break;
            }
        }
        if (title) {
            filter.$or = [
                { province: { $regex: title, $options: 'i' } },
                { city: { $regex: title, $options: 'i' } },
                { district: { $regex: title, $options: 'i' } }
            ];
        }
        if (status === 1) {
            filter.status = 1;
        } else if (status === 0) {
            filter.status = 0;
        }

        let data = await this.queryPagination(offset, limit, {
            filter: filter,
            populateOptions: {
                path: 'createuser',
                select: 'name'
            }
        });
        data.rows = data.rows.map((item) => {
            if (item.status === 1) {
                item.status = '有效';
            } else {
                item.status = '禁用';
            }
            return item;
        })
        return data;
    }
    async save(doc: any) {
        if (doc._id) {
            this.update(doc._id, doc);
        } else {
            this.create(doc);
        }
        return '保存成功';
    }

    async getsetward(puser: any) {
        // 查询是否有当前城市奖励配置
        let ward1, ward2, ward3;
        if (puser.province) {
            ward1 = await invitationawardService.querySingle({ status: 1, province: puser.province, city: { $exists: false }, district: { $exists: false } });
            if (puser.city) {
                ward2 = await invitationawardService.querySingle({ status: 1, province: puser.province, city: puser.city, district: { $exists: false } });
                if (puser.district) {
                    ward3 = await invitationawardService.querySingle({ status: 1, province: puser.province, city: puser.city, district: puser.district });
                }
            }
            if (ward3) {
                return ward3;
            } else if (ward2) {
                return ward2;
            } else if (ward1) {
                return ward1;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

export const invitationawardService = new InvitationawardService();